package com.dyz.wsw.domain.admin.convert;

import com.dyz.wsw.domain.admin.pojo.UserAuth;
import io.r2dbc.spi.Row;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author dyz
 * @date 2025/1/13 18:00
 */
@ReadingConverter
@Slf4j
public class UserAuthConvert implements Converter<Row, UserAuth> {

    public UserAuthConvert(){
        log.debug("创建自定义转换器【{}】", UserAuthConvert.class.getName());
    }

    @Override
    public UserAuth convert(Row source) {
        UserAuth userAuth = new UserAuth();
        userAuth.setId(source.get("id", Long.class));
        userAuth.setUsername(source.get("username", String.class));
        userAuth.setPassword(source.get("password", String.class));
        userAuth.setAvatar(source.get("avatar", String.class));
        Integer enable = source.get("enable", Integer.class);
        if (enable != null) {
            userAuth.setEnable( enable == 1);
        }
        List<Object> list = source.get("value", List.class);
        List<String> permissions = list.stream().map(Object::toString).toList();
        userAuth.setPermission(permissions);
        return userAuth;
    }
}
